

tree.io.rpc.JSON_Proxy = Class({
	/*
	Proxy for soapjr server methods.
	For the standard look here: http://www.soapjr.org/specs.html
	
	events: ['error-occured'],
	
	*/
	
	extend: tree.io.rpc.Proxy,
	
	init: function(url)
	{
	    tree.io.rpc.Proxy.apply(this, [url]);
	    this.content_type = tree.io.ajax.DATA_FORMAT.json;
	},
    
    parse_response: function(data)
    {
        var response = tree.io.formats.json.load(data);
        /*
        example: 
{
    "HEAD" : {
        "result" : 1
    },
    "BODY" : {
        "email" : [
            {
                "type" : ["internet","pref"],
                "value" : "jbloggs@SOAPjr.org"
            }
        ],
        "fn" : "Joe Bloggs",
        "kind" : "individual",
        "n" : {
            "family-name" : ["Bloggs"],
            "given-name" : ["Joe"],
            "value" : "Bloggs;Joe"
        },
        "org" : [
            {
                "organization-name" : "SOAPjr.org"
            }
        ]
    }
}
        */
        if(response.HEAD.result == 0)
        {
            this.emit('error-occured', {error: response.error});
            return;
        }
        return response.BODY;
    },
    
    create_request: function(name, params)
    {
        var request = {};
        /*
        example: { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
        */
        if(this.version == '1.0')
        {
            request.id = 1;
            request.method = name;
            request.params = params;
        }
        /*
        { 
            "version": "1.1", 
            "method": "confirmFruitPurchase", 
            "id": "194521489", 
            "params": 
            [ 
                [ "apple", "orange", "pear" ], 
                1.123 
            ]
        }
        */
        else if(this.version == '1.1')
        {
            request.id = 1;
            request.version = '1.1';
            request.method = name;
            request.params = params;
        }
        
        else if(this.version == '2.0')
        {
            
        }
        
        else
        {
            throw 'invalid json-rpc version: ' + this.version;
        }
        
        return tree.io.formats.json.dump(request);
    },
});
